package com.wzq.animal.fore.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzq.animal.common.Result;
import com.wzq.animal.fore.pojo.Website;
import com.wzq.animal.fore.pojo.Website;
import com.wzq.animal.fore.service.WebsiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/website")
public class WebsiteController {
    @Autowired
    private WebsiteService websiteService;

    @GetMapping("/getWebsites")
    private Result getWebsites(){
        List<Website> websites = websiteService.list();
        return Result.success(websites);
    }

    @GetMapping("/getPages/{currentPage}/{pageSize}")
    public Result getPages(@PathVariable("currentPage") int currentPage,
                           @PathVariable("pageSize") int pageSize,
                           @RequestParam("nameKeywords") String nameKeywords){
        Page<Website> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<Website> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(nameKeywords)) {
            queryWrapper.like(Website::getName, nameKeywords);
        }
        websiteService.page(page, queryWrapper);
        return Result.success(page);
    }

    @PostMapping("/addOrUpdateWebsite")
    public Result addOrUpdateWebsite(@RequestBody Website website) {
        websiteService.saveOrUpdate(website);
        Integer id = website.getId();
        if(StringUtils.isNotBlank(id.toString())){
            Website updatedWebsite = websiteService.getById(id);
            return Result.success(updatedWebsite);
        }
        return Result.success();
    }

    @DeleteMapping("/delWebsite/{id}")
    public Result delWebsite(@PathVariable("id") int id) {
        websiteService.removeById(id);
        return Result.success();
    }

    @DeleteMapping("/delWebsiteBatch")
    public Result delBatch(@RequestBody List<Integer> ids) {
        websiteService.removeByIds(ids);
        return Result.success();
    }
}
